﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Data;
using COP.Extensions;
using System.Collections;
using COP.Extractors;
using COP.Objects.Maps;

namespace COP.Objects.Config
{
    public class BooleanConfig : ConfigBase
    {
        public BooleanConfig(IExtractor extractor)
            : this(TagType.Boolean, extractor) { }


        public BooleanConfig(TagType type, IExtractor extractor)
            : base(type,extractor) { }

        protected override string CreateKey(object config)
        {
            return this.extractor.ExtractKey(config, IsConfigKeyMandate);
        }

        protected override IEnumerable ExtractSource(IEnumerable config)
        {
            return this.extractor.ExtractSource(config);
        }

        protected override List<MAPBase> CreateMAPCollection(IEnumerable data)
        {
            List<MAPBase> valList = new List<MAPBase>();
            foreach (object valNode in data)
            {
                MAPBase map = OnCreateSingleMAP(valNode);
                if (string.IsNullOrEmpty(map.Key) && (!string.IsNullOrEmpty(key)))
                    map.Key = key;
                valList.Add(map);
            }

            return valList;
        }

        protected virtual MAPBase OnCreateSingleMAP(object node)
        {
            MAPBase map = base.mapToCreate;
            map.Append(node);
            if (IsConfigKeyMandate && string.IsNullOrEmpty(map.Key))
                throw new Exception(Constant.Error.KeyRequired);
            else
                return map;
        }

        public override object PullValue()
        {
            return bool.Parse(this.Maps[0].Value.ToString());
        }

        protected override bool IsConfigKeyMandate
        {
            get
            {
                return false;
            }
        }


       

    }
}